package com.example.javatest.controller;

import com.example.javatest.entity.domain.SupSubmit;
import com.example.javatest.entity.domain.request.ClassAddRequest;
import com.example.javatest.entity.domain.request.RoleRequest;
import com.example.javatest.service.SupAddclassService;
import com.example.javatest.service.SupSubmitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/submit")
@CrossOrigin
public class SubmitController {
    @Resource
    private SupSubmitService supSubmitService;

    @Resource
    private SupAddclassService supAddclassService;

    /**
     * 查询所有身份申请
     * @return
     */
    @GetMapping("/search")
    public List<SupSubmit> searchUsers(){
        return supSubmitService.list();
    }
    /**
     * 查询所有 身份更改 申请
     * @param roleRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/submitRole")
    public boolean submit(@RequestBody RoleRequest roleRequest, HttpServletRequest httpServletRequest){
        if(roleRequest==null){
            return false;
        }
        int id = roleRequest.getUserID();
        String role = roleRequest.getSubRole();
        String code = roleRequest.getSubCode();
        if(role == null && id<0){
            return false;
        }
        return supSubmitService.submit(code,role,id);
    }

    /**
     * 身份申请处理
     * @param roleRequest
     * @return
     */
    @PostMapping("/handleRoleSubmit")
    public boolean handleRoleSubmit(@RequestBody RoleRequest roleRequest, HttpServletRequest httpServletRequest){

        if(roleRequest==null){
            return false;
        }
        int ID = roleRequest.getUserID();
        String isHandle = roleRequest.getIsHandle();
        System.out.println(ID);
        System.out.println(isHandle);
        return  supSubmitService.supHandle(isHandle,ID);
    }

    /**
     * 家长加入班级申请提交
     */
    @PostMapping("/submitAddClass")
    public boolean submitAddClass(@RequestBody ClassAddRequest classAddRequest,HttpServletRequest httpServletRequest){
        if(classAddRequest==null){
            return false;
        }
        String className = classAddRequest.getAddClassName();
        String userName = classAddRequest.getUser();
        if(StringUtils.isAnyBlank(userName,className)){
            return false;
        }
        return supAddclassService.addClass(className,userName);
    }
    /**
     * 处理家长加入班级的申请
     */
    @PostMapping("/handleAddClass")
    public boolean handleAddClass(@RequestBody ClassAddRequest classAddRequest,HttpServletRequest httpServletRequest){
        if(classAddRequest==null){
            System.out.println("1111111111111");
            return false;
        }
        int cup_Id = classAddRequest.getCupId();
        String isHandle = classAddRequest.getIsHandel();
        if(cup_Id==0 || isHandle == null){
            System.out.println("22222222222");
            return false;
        }
        return supAddclassService.handleAddClassAply(cup_Id, isHandle);
    }
}
